package com.jing.cloud.client;

import com.jing.cloud.service.Rsp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ClientProxy {

    private static final Logger logger = LoggerFactory.getLogger(ClientProxy.class);


    public static void main(String[] args) throws Exception {
        int corePoolSize = 24;
        int maximumPoolSize = 24;
        long keepAliveTime = 0;
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
        ;
        RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
        ExecutorService executor = new ThreadPoolExecutor
                (corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, workQueue, handler);

        Request request = new Request();
        request.setServiceName("userService");
        request.setMethodName("addUser");
        request.setParam("{'username':'jing','password':'123'}");
        Random r = new Random();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 2; i++) {
            if (i % 2 == 0) {

                request.setMethodName("updateUser");
            } else {
                request.setMethodName("addUser");
            }
            logger.info("" + i);
//            executor.execute(new Runnable() {
//                
//                @Override
//                public void run() {
//                   
//                }
//            });
            Rsp rsp = ServiceUtil.call(request);
            logger.info(rsp.toString());

        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }


}
